import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      name: 'Layout',
      component: () => import('@/layout/SimpleLayout.vue'),
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: '/dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { title: '仪表盘', icon: 'Dashboard' }
        },
        {
          path: '/system',
          name: 'System',
          component: () => import('@/views/system/index.vue'),
          meta: { title: '系统管理', icon: 'Setting' },
          children: [
            {
              path: '/system/users',
              name: 'UserManagement',
              component: () => import('@/views/system/UserManagement.vue'),
              meta: { title: '用户管理', icon: 'User' }
            },
            {
              path: '/system/roles',
              name: 'RoleManagement',
              component: () => import('@/views/system/RoleManagement.vue'),
              meta: { title: '角色管理', icon: 'UserFilled' }
            },
            {
              path: '/system/permissions',
              name: 'PermissionManagement',
              component: () => import('@/views/system/PermissionManagement.vue'),
              meta: { title: '权限管理', icon: 'Key' }
            },
            {
              path: '/system/menus',
              name: 'MenuManagement',
              component: () => import('@/views/system/MenuManagement.vue'),
              meta: { title: '菜单管理', icon: 'Menu' }
            }
          ]
        },
        {
          path: '/cmdb',
          name: 'CMDB',
          component: () => import('@/views/cmdb/index.vue'),
          meta: { title: 'CMDB资产管理', icon: 'Box' },
          children: [
            {
              path: '/cmdb/hosts',
              name: 'HostManagement',
              component: () => import('@/views/cmdb/HostManagement.vue'),
              meta: { title: '主机管理', icon: 'Monitor' }
            },
            {
              path: '/cmdb/host-groups',
              name: 'HostGroupManagement',
              component: () => import('@/views/cmdb/HostGroupManagement.vue'),
              meta: { title: '主机组管理', icon: 'Collection' }
            },
            {
              path: '/cmdb/environments',
              name: 'EnvironmentManagement',
              component: () => import('@/views/cmdb/EnvironmentManagement.vue'),
              meta: { title: '环境管理', icon: 'Setting' }
            },
            {
              path: '/cmdb/host-types',
              name: 'HostTypeManagement',
              component: () => import('@/views/cmdb/HostTypeManagement.vue'),
              meta: { title: '主机类型管理', icon: 'Cpu' }
            },
            {
              path: '/cmdb/enhanced-hosts',
              name: 'EnhancedHostManagement',
              component: () => import('@/views/cmdb/EnhancedHostManagement.vue'),
              meta: { title: '增强主机管理', icon: 'Monitor' }
            },
            {
              path: '/cmdb/monitoring',
              name: 'MonitoringDashboard',
              component: () => import('@/components/monitoring/MonitoringDashboard.vue'),
              meta: { title: '监控仪表盘', icon: 'TrendCharts' }
            },
            {
              path: '/cmdb/ssh-terminal',
              name: 'SSHTerminalPage',
              component: () => import('@/views/cmdb/SSHTerminalPage.vue'),
              meta: { title: 'SSH终端', icon: 'Monitor' }
            },
            {
              path: '/cmdb/hosts/:id',
              name: 'HostDetail',
              component: () => import('@/views/cmdb/HostDetail.vue'),
              meta: { title: '主机详情', icon: 'View', hidden: true }
            },
            {
              path: '/test/monitoring',
              name: 'MonitoringTest',
              component: () => import('@/views/test/MonitoringTest.vue'),
              meta: { title: '监控测试', icon: 'Setting', hidden: true }
            }
          ]
        },
        {
          path: '/database',
          name: 'Database',
          component: () => import('@/views/database/index.vue'),
          meta: { title: '数据库管理', icon: 'Coin' },
          children: [
            {
              path: '/database/overview',
              name: 'DatabaseOverview',
              component: () => import('@/views/database/DatabaseManagement.vue'),
              meta: { title: '数据库概览', icon: 'DataBoard' }
            },
            {
              path: '/database/connections',
              name: 'DatabaseConnections',
              component: () => import('@/views/database/ConnectionManagement.vue'),
              meta: { title: '连接管理', icon: 'Connection' }
            },
            {
              path: '/database/query',
              name: 'DatabaseQuery',
              component: () => import('@/views/database/QueryTool.vue'),
              meta: { title: 'SQL查询', icon: 'EditPen' }
            },
            {
              path: '/database/backup',
              name: 'DatabaseBackup',
              component: () => import('@/views/database/BackupManagement.vue'),
              meta: { title: '备份管理', icon: 'Download' }
            },
            {
              path: '/database/monitoring',
              name: 'DatabaseMonitoring',
              component: () => import('@/views/database/MonitoringDashboard.vue'),
              meta: { title: '性能监控', icon: 'Monitor' }
            }
          ]
        },
        {
          path: '/monitor',
          name: 'Monitor',
          component: () => import('@/views/monitor/index.vue'),
          meta: { title: '运维监控', icon: 'TrendCharts' },
          children: [
            {
              path: '/monitor/system',
              name: 'SystemMonitor',
              component: () => import('@/views/monitor/SystemMonitor.vue'),
              meta: { title: '系统监控', icon: 'Cpu' }
            },
            {
              path: '/monitor/logs',
              name: 'LogManagement',
              component: () => import('@/views/monitor/LogManagement.vue'),
              meta: { title: '日志管理', icon: 'Document' }
            }
          ]
        },
        {
          path: '/zabbix',
          name: 'Zabbix',
          component: () => import('@/views/zabbix/Dashboard.vue'),
          meta: { title: 'Zabbix监控', icon: 'Monitor' }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查本地存储中的token
  const token = localStorage.getItem('access_token')
  const isAuthenticated = !!token

  console.log('路由守卫:', {
    to: to.path,
    from: from.path,
    isAuthenticated,
    requiresAuth: to.meta.requiresAuth
  })

  // 如果访问需要认证的页面但未登录，跳转到登录页
  if (to.meta.requiresAuth !== false && !isAuthenticated) {
    console.log('未认证，跳转到登录页')
    next('/login')
  }
  // 如果已登录但访问登录页，跳转到首页
  else if (to.path === '/login' && isAuthenticated) {
    console.log('已认证，跳转到首页')
    next('/')
  }
  // 其他情况正常通过
  else {
    console.log('正常通过')
    next()
  }
})

export default router
